package cn.donting.jme.edit.ui.toolbar;

import cn.donting.jme.edit.EditorState;
import cn.donting.jme.edit.ex.ExResourcesLoad;
import cn.donting.jme.edit.ex.ToolBarUILoader;
import com.jme3.scene.Node;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class ToolBarManger implements Initializable {
    @FXML
    public HBox hBox;
    /**
     * ToolBar组
     */
    private ToggleGroup toggleGroup = new ToggleGroup();

    /**
     * 激活的actionToolBar
     */
    private ToolBar actionToolBar;
    /**
     * 激活的 actionNode
     */
    private Node actionNode;
    /**
     * JmeEditSceneApp 的 编辑器节点
     */
    private Node editorNode;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        editorNode = EditorState.getJmeEditSceneApp().getEditorNode();
        List<ToolBarUILoader> toolBarUILoader = ExResourcesLoad.getToolBarUILoader();
        toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
            if (actionToolBar != null) {
                editorNode.detachChild(actionNode);
            }
            actionToolBar = (ToolBar) newValue;
            actionNode = actionToolBar.getJmeControlNode();
            editorNode.attachChild(actionNode);
        });
        for (ToolBarUILoader barUILoader : toolBarUILoader) {
            ArrayList<ToolBar> toolBars = barUILoader.loadToolBar();
            for (ToolBar toolBar : toolBars) {
                toolBar.setToggleGroup(toggleGroup);
                toolBar.init(EditorState.getJmeEditSceneApp().getAssetManager(), EditorState.getJmeEditSceneApp().getInputManager());
                hBox.getChildren().add(toolBar);
            }
        }
        //默认第一个激活
        ToolBar nullTooNar = (ToolBar)hBox.getChildren().get(0);
        nullTooNar.selectedProperty().set(true);
    }
}
